package com.uwosh.cs342.g6.mapapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.uwosh.cs342.g6.mapapp.MainActivity.checkFriendRequests;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Friendrequest extends Activity {
	String usernameThatSentRequest;
	
	Context c;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friendrequest);
		
		c = this;
		
		
		Intent intent = getIntent();
		usernameThatSentRequest = intent.getStringExtra("name");
		
		TextView user = (TextView)findViewById(R.id.textView1);
		String[] temp = new String[2];
		temp = MainActivity.usernameThatSentTheFriendRequest.split(":");
		Log.d(temp[0],"IAMHERE");
		user.setText(temp[0]);
		


	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_friendrequest, menu);
		return true;
	}
	
	public void finish(View v)
	{
		this.finish();
		Intent MainActivity = new Intent(this, MainActivity.class);

	    startActivity(MainActivity);
	}
	
	public void accept(View v)
	{
		try {
			boolean x = new acceptRequest().execute(getApplicationContext()).get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.finish();
		Intent MainActivity = new Intent(this, MainActivity.class);

	    startActivity(MainActivity);		
	}
	
	public class acceptRequest extends AsyncTask<Context, Context, Boolean> {

		@Override
		protected Boolean doInBackground(Context... params) {

			int userNum = Login.currentUserId;
			String response;
			String[] temp2 = new String[2];
			temp2 = MainActivity.usernameThatSentTheFriendRequest.split(":");
			int otherUser = Integer.parseInt(temp2[1]);
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(
					"http://softeng.cs.uwosh.edu/students/cs342g6/makeFriends.php?one="+userNum+"&two="+otherUser);
			try {

				// Execute HTTP Post Request

				ResponseHandler<String> responseHandler = new BasicResponseHandler();
				response = httpclient.execute(httppost, responseHandler);
				int responseInt = Integer.parseInt(response);

				if (responseInt == 1) {
					
					return true;
				}
				else
				{
					return false;
				}

			} catch (ClientProtocolException e) {
				// Toast.makeText(params[0], "CPE response " +
				// e.toString(), Toast.LENGTH_LONG).show();
				// TODO Auto-generated catch block
			} catch (IOException e) {

				// TODO Auto-generated catch block
			}
			return false;
		}

		@Override
		protected void onPostExecute(Boolean values) {
			Toast.makeText(c, "You're now friends", Toast.LENGTH_SHORT).show();
			

		}
	}}



